from PyQt4.QtGui import QSystemTrayIcon, qApp, QAction, QStyle, QMenu
from PyQt4.QtCore import QEvent, Qt
from PyQt4.QtCore import QEvent
from PyQt4 import QtCore
import sys

class TrayIcon(QSystemTrayIcon):
    clicked = QtCore.pyqtSignal(int)

    def __init__(self, mw):
        QSystemTrayIcon.__init__(self)

        trayMenu = QMenu(mw)

        self.actionNext = QAction(self.tr('Next'), self)
        trayMenu.addAction(self.actionNext)

        self.actionPrev = QAction(self.tr('Prev'), self)
        trayMenu.addAction(self.actionPrev)

        trayMenu.addSeparator()
        self.actionQuit = QAction(self.tr('Quit'), self)
        self.actionQuit.triggered.connect(qApp.quit)
        trayMenu.addAction(self.actionQuit)

        self.setContextMenu(trayMenu)
        self.setIcon(mw.style().standardIcon(QStyle.SP_MediaStop))
        self.show()

        self.activated.connect(self.trayClicked)

    def setParent(self, parent):
        self.phonik = parent

    def trayClicked(self, reason):
        self.clicked.emit(reason)


    def eventFilter(self, object, event):
        if object is self and event.type() == QEvent.Wheel:
            delta = event.delta()


            #if the middle mouse button is held
            if qApp.mouseButtons() == Qt.MidButton:
                if delta > 0:
                    self.phonik.prevSong()
                else:
                    self.phonik.nextSong()
            else:
                if delta > 0:
                #increase volume
                    self.phonik.mw.volumeBar.setValue(self.phonik.mw.volumeBar.value() + 10)
                else:
                #decrease volume
                    self.phonik.mw.volumeBar.setValue(self.phonik.mw.volumeBar.value() - 10)

                #self.showMessage(self.tr('Volume'), str(self.phonik.py.mw.volumeBar.value() ) + '%', QSystemTrayIcon.NoIcon, 700)

            event.accept()
            return True

        return False